package com.zhuss.want.processor;

import java.util.Date;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.want.want.DeleteWantAppendBo;
import com.zhuss.bean.bo.want.want.wechat.CollectWantBo;
import com.zhuss.bean.vo.want.want.DetailWantAppendVo;
import com.zhuss.bean.vo.want.want.DetailWantVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.exception.WpsException;
import com.zhuss.util.JsonUtil;

/**
 * 取消收藏求职 processor
 * @Author: zhushanshuo
 * @Date: 2020/02/28 16:01
 */
@Component("wantDelCollect")
public class WantDelCollect extends AbstractProcessor<Object, CollectWantBo> implements Processor<Object, CollectWantBo> {

    @Autowired
	@Qualifier("wantRead")
	private WantRead wantRead;
    
    @Autowired
	@Qualifier("wantAppendRead")
	private WantAppendRead wantAppendRead;
    
    @Autowired
    @Qualifier("wantAppendDelete")
    private Processor<Object, DeleteWantAppendBo> wantAppendDelete;
    
    /**
     * 校验
     */
    @Override
    public void check(CollectWantBo bo) throws WpsException {
    	Long wantId = bo.getWantId();
    	Integer appendType = bo.getAppendType();
    	
    	DetailWantAppendVo result = wantAppendRead.getByType(appendType, wantId, bo.getConcurrentUserId());
    	
    	if (result == null) {
    		throw new WpsException(ExceptionCode.求职未收藏);
    	}
    	
    }
    
    /**
     * 操作方法
     * @throws WpsException 
     */
    @Override
    public Object process(CollectWantBo param) throws WpsException {
    	Long wantId = param.getWantId();
    	Integer appendType = param.getAppendType();
    	
    	DetailWantAppendVo result = wantAppendRead.getByType(appendType, wantId, param.getConcurrentUserId());
    	
    	DetailWantVo want = ((DetailWantVo) wantRead.detail(wantId));
    	String name = want.getTitle();
    	
    	DeleteWantAppendBo bo = new DeleteWantAppendBo();
    	BeanUtils.copyProperties(param, bo);
    	bo.setWantAppendId(result.getWantAppendId());
    	wantAppendDelete.process(bo);
    	
    	param.setName(name);
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(CollectWantBo param) throws WpsException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.求职.key());
    	record.setAction(LogActionEnum.求职取消收藏.key());
    	record.setTargetId(param.getWantId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
